// $ANTLR 3.1.3 Mar 17, 2009 19:23:44 CSVParser.g 2010-01-30 20:46:23
package org.concepts.java.antlr.csv;

import java.util.HashMap;
import java.util.Map;



import org.antlr.runtime.*;
import java.util.Stack;
import java.util.List;
import java.util.ArrayList;

/**
 Parser Grammar file
 -------------------

 Parser process the tokens sent to it on a channel by the lexer. Processing
 involves application of rules to the token.
 */
public class CSVParser extends Parser {
    public static final String[] tokenNames = new String[] {
        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "COMMA", "INT", "WHITESPACE", "NEWLINE", "SINGLE_COMMENT", "MULTI_COMMENT"
    };
    public static final int NEWLINE=7;
    public static final int COMMA=4;
    public static final int MULTI_COMMENT=9;
    public static final int WHITESPACE=6;
    public static final int INT=5;
    public static final int SINGLE_COMMENT=8;
    public static final int EOF=-1;

    // delegates
    // delegators


        public CSVParser(TokenStream input) {
            this(input, new RecognizerSharedState());
        }
        public CSVParser(TokenStream input, RecognizerSharedState state) {
            super(input, state);
             
        }
        

    public String[] getTokenNames() { return CSVParser.tokenNames; }
    public String getGrammarFileName() { return "CSVParser.g"; }



      private int recordCount = 0;
      
      private Map<Integer,int[]> recordData = new HashMap<Integer,int[]>();

      /**
      * Returns the number of records in the list 
      */
      public int getRecordCount() {
        return recordCount;
      }
      
      /**
       * Returns an array of integers for the given record.  
       *  
       * @param counter record counter. E.g. If there are 3 records, then
       * valid counter are 0,1,2
       * @return array of integer. Is null if invalid counter is passed in.
       */
      public int[] getRecord(int counter)  {
        return (int[]) recordData.get(counter);
      }  
      



    // $ANTLR start "file"
    // CSVParser.g:106:1: file : ( record )+ ;
    public final void file() throws RecognitionException {
        try {
            // CSVParser.g:109:5: ( ( record )+ )
            // CSVParser.g:109:7: ( record )+
            {
            // CSVParser.g:109:7: ( record )+
            int cnt1=0;
            loop1:
            do {
                int alt1=2;
                int LA1_0 = input.LA(1);

                if ( (LA1_0==INT) ) {
                    alt1=1;
                }


                switch (alt1) {
            	case 1 :
            	    // CSVParser.g:109:7: record
            	    {
            	    pushFollow(FOLLOW_record_in_file68);
            	    record();

            	    state._fsp--;


            	    }
            	    break;

            	default :
            	    if ( cnt1 >= 1 ) break loop1;
                        EarlyExitException eee =
                            new EarlyExitException(1, input);
                        throw eee;
                }
                cnt1++;
            } while (true);


            }

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "file"


    // $ANTLR start "record"
    // CSVParser.g:111:1: record : (first= INT ) ( COMMA (other= INT ) )* ( NEWLINE | EOF ) ;
    public final void record() throws RecognitionException {
        Token first=null;
        Token other=null;


          List<Integer> integers = new ArrayList<Integer>();

        try {
            // CSVParser.g:129:3: ( (first= INT ) ( COMMA (other= INT ) )* ( NEWLINE | EOF ) )
            // CSVParser.g:129:5: (first= INT ) ( COMMA (other= INT ) )* ( NEWLINE | EOF )
            {
            // CSVParser.g:129:5: (first= INT )
            // CSVParser.g:129:6: first= INT
            {
            first=(Token)match(input,INT,FOLLOW_INT_in_record96); 

            }

             integers.add(new Integer((first!=null?first.getText():null)));
            // CSVParser.g:131:5: ( COMMA (other= INT ) )*
            loop2:
            do {
                int alt2=2;
                int LA2_0 = input.LA(1);

                if ( (LA2_0==COMMA) ) {
                    alt2=1;
                }


                switch (alt2) {
            	case 1 :
            	    // CSVParser.g:131:6: COMMA (other= INT )
            	    {
            	    match(input,COMMA,FOLLOW_COMMA_in_record115); 
            	    // CSVParser.g:131:12: (other= INT )
            	    // CSVParser.g:131:13: other= INT
            	    {
            	    other=(Token)match(input,INT,FOLLOW_INT_in_record122); 

            	    }

            	    integers.add(new Integer((other!=null?other.getText():null)));

            	    }
            	    break;

            	default :
            	    break loop2;
                }
            } while (true);

            if ( input.LA(1)==EOF||input.LA(1)==NEWLINE ) {
                input.consume();
                state.errorRecovery=false;
            }
            else {
                MismatchedSetException mse = new MismatchedSetException(null,input);
                throw mse;
            }


            }


              int[] intArray = new int[integers.size()];
              int intCtr = 0;
              for (Integer integer : integers) {
                intArray[intCtr++] = integer.intValue();
              }

              recordData.put(recordCount++, intArray);

        }
        catch (RecognitionException re) {
            reportError(re);
            recover(input,re);
        }
        finally {
        }
        return ;
    }
    // $ANTLR end "record"

    // Delegated rules


 

    public static final BitSet FOLLOW_record_in_file68 = new BitSet(new long[]{0x0000000000000022L});
    public static final BitSet FOLLOW_INT_in_record96 = new BitSet(new long[]{0x0000000000000090L});
    public static final BitSet FOLLOW_COMMA_in_record115 = new BitSet(new long[]{0x0000000000000020L});
    public static final BitSet FOLLOW_INT_in_record122 = new BitSet(new long[]{0x0000000000000090L});
    public static final BitSet FOLLOW_set_in_record146 = new BitSet(new long[]{0x0000000000000002L});

}